/************************************
 * Package: RGcpp					*
 * Authors: Rui Eduardo Gouveia Gil	*
 * Date: 2010						*
 ************************************/
#include "rgcpp/tools/SHAGenerator.h"

namespace rgcpp
{

SHAGenerator::SHAGenerator() : state(false)
{

}

SHAGenerator::~SHAGenerator()
{

}

void SHAGenerator::init()
{
	state=true;
	SHA256_Init(&sha_ctx);
}

void SHAGenerator::update(const char* data, const unsigned length)
{
	if(!state)
	{
		Throw(IllegalStateException("SHAGenerator not initialized!!!"));
	}

	SHA256_Update(&sha_ctx,data,length);
}

void SHAGenerator::digest(char* digest)
{
	if(!state)
	{
		Throw(IllegalStateException("SHAGenerator not initialized!!!"));
	}

	SHA256_Final((unsigned char*)digest,&sha_ctx);
	state=false;
}

void SHAGenerator::digest(const char* data, const unsigned int length, char* digest)
{
	SHA256_CTX sha_ctx;

	SHA256_Init(&sha_ctx);
	SHA256_Update(&sha_ctx,data,length);
	SHA256_Final((unsigned char*)digest,&sha_ctx);
}

}
